package br.uninove.ic.texture;

import br.uninove.ic.texture.utils.ImagesUtils;

public class MatrizMomentoDoHistograma {

	
	private double[] histograma = new double[256];
	
	
	
	public MatrizMomentoDoHistograma(IGrayScaleImage img) {
		
		// monta o histograma
		for (int x = 0; x < img.getWidth(); x++) {
			for (int y = 0; y < img.getHeight(); y++) {
				if (ImagesUtils.isIndexImage(img, x, y)) {
					histograma[img.getPixel(x, y)]++;
				}
			}
		}
	}
	
	
	
	/**
	 * Retorna os momentos do histograma
	 * @param n
	 * 		momento 1, 2, 3 ou 4
	 * @return
	 */
    public double getMomento(int n) {
    	double momento = 0;
    	double media = getMedia();
    	
    	for (int i = 0;i<histograma.length;i++) {
    		momento += Math.pow((i - media),n) * histograma[i];
    	}

    	return momento;
    }
    
	
	
	
	public double getMedia() {
    	double media = 0;
    	for (int i = 0;i<histograma.length;i++)
    	{
    		media+=histograma[i];
    	}
    	return media/histograma.length;
    }
}
